import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:riverpod_t/widget/custom_text.dart';

class CustomButton extends StatelessWidget {
  ///按钮宽度
  final double? width;

  ///按钮高度
  final double? height;

  ///对齐方式
  final MainAxisAlignment mainAxisAlignment;
  final MainAxisSize mainAxisSize;
  final CrossAxisAlignment crossAxisAlignment;

  ///icon名称
  final String? iconName;

  ///icon宽
  final double? iconWidth;

  ///icon高
  final double? iconHeight;

  ///icon颜色
  final Color? iconColor;

  ///icon模式
  final BoxFit? iconFit;

  ///icon间距
  final double? iconSpace;

  ///按钮文字
  final String? text;

  ///文字样式
  final TextStyle? textStyle;

  ///字体颜色
  final Color? textColor;

  ///圆角半径
  final double? borderRadius;

  ///边框颜色
  final Color? borderColor;

  ///边框宽度
  final double? borderWidth;

  ///背景颜色borderColor
  final Color? backgroundColor;

  ///渐变色
  final Gradient? gradient;

  ///阴影高度
  final List<BoxShadow>? boxShadow;

  ///外边距
  final EdgeInsetsGeometry? margin;

  ///外边距
  final EdgeInsetsGeometry? padding;

  ///文字大小
  final double? fontSize;

  ///字重
  final FontWeight fontWeight;

  ///事件回调
  final VoidCallback? onPressed;

  /// 是否显示水波纹
  final bool isRipples;

  final BoxConstraints? constraints;

  const CustomButton({
    super.key,
    this.width,
    this.height,
    this.mainAxisAlignment = MainAxisAlignment.center,
    this.crossAxisAlignment = CrossAxisAlignment.center,
    this.mainAxisSize = MainAxisSize.max,
    this.iconName,
    this.iconWidth,
    this.iconHeight,
    this.iconColor,
    this.iconFit,
    this.iconSpace,
    this.text,
    this.textStyle,
    this.textColor,
    this.borderRadius,
    this.borderColor,
    this.borderWidth,
    this.backgroundColor,
    this.gradient,
    this.boxShadow,
    this.margin,
    this.padding,
    this.fontSize,
    this.fontWeight = FontWeight.normal,
    this.onPressed,
    this.isRipples = true,
    this.constraints,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      width: width,
      height: height ?? 44.h,
      margin: margin,
      padding: padding,
      constraints: constraints,
      decoration: _boxDecoration(),
      child: _elevatedButton(),
    );
  }

  ///内容视图
  Widget _elevatedButton() {
    return ElevatedButton(
      onPressed: onPressed,
      style: ElevatedButton.styleFrom(
        // 内边距为零
        padding: EdgeInsets.zero,
        // 阴影透明
        shadowColor: Colors.transparent,
        // 背景透明
        backgroundColor: Colors.transparent,
        //禁用背景透明
        disabledBackgroundColor: Colors.transparent,
        //涟漪效果
        splashFactory: isRipples ? InkRipple.splashFactory : NoSplash.splashFactory,
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(borderRadius ?? 22.r)),
      ),
      child: _buildChild(),
    );
  }

  ///装饰容器
  BoxDecoration _boxDecoration() {
    Border? border;
    if (borderColor != null) {
      border = Border.all(color: borderColor!, width: borderWidth ?? 0);
    }
    return BoxDecoration(
      gradient: gradient,
      color: backgroundColor,
      borderRadius: BorderRadius.circular(borderRadius ?? 22.r),
      border: border,
      boxShadow: boxShadow,
    );
  }

  /// 构建按钮内容
  Widget _buildChild() {
    return Row(
      mainAxisSize: mainAxisSize,
      mainAxisAlignment: mainAxisAlignment,
      crossAxisAlignment: crossAxisAlignment,
      children: [if (iconName != null) ..._beforeWidget(), _textWidget()],
    );
  }

  ///前置图片
  List<Widget> _beforeWidget() {
    var image = Image.asset(
      iconName ?? '',
      width: iconWidth ?? 16.w,
      height: iconHeight ?? 16.h,
      color: iconColor,
      fit: iconFit,
    );
    var space = SizedBox(width: iconSpace ?? 3.w);
    return [image, space];
  }

  ///文本内容
  Widget _textWidget() {
    return CustomText(
      text: text ?? '',
      fontSize: fontSize ?? 16.sp,
      fontWeight: fontWeight,
      textStyle: textStyle,
      color: textColor,
    );
  }
}
